package com.example.wangyeming.getuidemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.example.wangyeming.getuidemo.application.GetuiApplication;
import com.example.wangyeming.getuidemo.model.WaitBus;
import com.example.wangyeming.getuidemo.utils.MyNotification;
import com.igexin.sdk.PushConsts;
import com.igexin.sdk.PushManager;


public class MainActivity extends ActionBarActivity {

    private static final int FIRST_LINE_ID = 1;
    private static final int SAECOND_LINE_ID = 2;
    private static final int THIRD_LINE_ID = 3;

    private MyNotification myNotification;

    IntentFilter intentFilter = new IntentFilter();
    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            Log.d("GetuiSdkDemo", "onReceive() action=" + bundle.getInt("action"));
            switch (bundle.getInt(PushConsts.CMD_ACTION)) {
                case PushConsts.GET_MSG_DATA:
                    byte[] payload = bundle.getByteArray("payload");
                    if (payload != null) {
                        String data = new String(payload);
                        WaitBus waitBus = JSON.parseObject(data, WaitBus.class);
                        if (waitBus != null) {
                            showAlertDialog(waitBus);
                        }
                        Log.d("wym", "111111 显示dialog");
                    } else {
                        Log.d("wym", "111111 发送通知栏消息");
                    }
                    break;
                //添加其他case
                //.........
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onStart() {
        super.onStart();
        registerReceiver(broadcastReceiver, intentFilter);
        GetuiApplication.getInstance().setIsForeground(true);
    }

    @Override
    public void onStop() {
        super.onStop();
        unregisterReceiver(broadcastReceiver);
        GetuiApplication.getInstance().setIsForeground(false);
    }

    private void init() {
        PushManager.getInstance().initialize(this.getApplicationContext());
        intentFilter.addAction("com.igexin.sdk.action.JDa6ZkhoBU9vWW1FNQmvL");
        myNotification = new MyNotification(this);
    }

    private void showAlertDialog(WaitBus waitBus) {
        new AlertDialog.Builder(this)
                .setTitle("线路编号 " + waitBus.lineId)
                .setMessage(waitBus.context)
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show();
    }

    public void goSecond(View view) {
        Intent intent = new Intent(this, SecondActivity.class);
        startActivity(intent);
    }

    public void sendNotificationStyle1(View view) {
        WaitBus waitBus = new WaitBus("线路1即将到站", FIRST_LINE_ID, "车来了", 1, 1);
        myNotification.sendNotification(waitBus.lineId,
                0, MyNotification.NOTIFICATION_STYLE_1,
                "线路编号 " + waitBus.lineId, waitBus.context);
    }

    public void sendNotificationStyle2(View view) {
        WaitBus waitBus = new WaitBus("线路2即将到站", SAECOND_LINE_ID, "车来了", 1, 1);
        myNotification.sendNotification(waitBus.lineId,
                0, MyNotification.NOTIFICATION_STYLE_2,
                "线路编号 " + waitBus.lineId, waitBus.context);
    }

    public void sendNotificationStyle3(View view) {
        WaitBus waitBus = new WaitBus("线路3即将到站", THIRD_LINE_ID, "车来了", 1, 1);
        myNotification.sendNotification(waitBus.lineId,
                0, MyNotification.NOTIFICATION_STYLE_3,
                "线路编号 " + waitBus.lineId, waitBus.context);
    }

    public void removeAllNotification(View view) {
        myNotification.removeAllNotification();
    }

    public void clearNotificationStyle1(View view) {
        myNotification.removeNotification(FIRST_LINE_ID);
    }

    public void clearNotificationStyle2(View view) {
        myNotification.removeNotification(SAECOND_LINE_ID);
    }

    public void clearNotificationStyle3(View view) {
        myNotification.removeNotification(THIRD_LINE_ID);
    }
}
